
package com.iplant.basic.controller.bms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;

import com.iplant.base.po.excel.ExcelData;
import com.iplant.base.utils.excel.ExcelReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.BMSService;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.bms.BMSRole;
import com.iplant.basic.service.po.bms.BMSRoleItem;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/Role")
public class RoleController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(RoleController.class);
    @Autowired
    BMSService wBMSService;

    @GetMapping("/Tree")
    Object Tree(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            ServiceResult<List<BMSRoleItem>> wServerRst = wBMSService.BMS_GetFunctionNodeTree(wBMSEmployee, 1);

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/TreeAll")
    Object TreeAll(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wActive = StringUtils.parseInt(request.getParameter("Active"));

            ServiceResult<List<BMSRoleItem>> wServerRst = wBMSService.BMS_GetFunctionNodeTree(wBMSEmployee, wActive);

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/TreeUpdate")
    Object TreeUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BMSRoleItem wBMSRoleItem = CloneTool.Clone(wParam.get("data"), BMSRoleItem.class);

            ServiceResult<BMSRoleItem> wServerRst = wBMSService.BMS_UpdateFunctionNodeTree(wBMSEmployee, wBMSRoleItem);

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst.getResult());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, wServerRst.getResult());
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/TreeActive")
    Object TreeActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data") || !wParam.containsKey("Active")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            List<Integer> wFunctionIDList = CloneTool.CloneArray(wParam.get("data"), Integer.class);
            int wActive = StringUtils.parseInt(wParam.get("Active"));
            ServiceResult<Integer> wServerRst = wBMSService.BMS_ActiveFunctionNodeTree(wBMSEmployee, wFunctionIDList,
                    wActive);

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode());
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/TreeDelete")
    Object TreeDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            List<Integer> wFunctionIDList = CloneTool.CloneArray(wParam.get("data"), Integer.class);
            if (wFunctionIDList == null || wFunctionIDList.size() <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

//			ServiceResult<Integer> wServerRst = wBMSService.BMS_DeleteFunctionNodeTree(wBMSEmployee, wFunctionIDList);
            ServiceResult<Integer> wServerRst = wBMSService.BMS_DeleteFunctionNodeTree_V1(wBMSEmployee,
                    wFunctionIDList);

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode());
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/All")
    Object All(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            List<Integer> wDepartmentID = StringUtils.parseIntList(request.getParameter("DepartmentID"), ",");
            int wIsTop = StringUtils.parseInt(request.getParameter("IsTop"));
            int wUserID = StringUtils.parseInt(request.getParameter("UserID"));
            String wName = StringUtils.parseString(request.getParameter("Name"));
            int wActive = StringUtils.parseInt(request.getParameter("Active"));

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<BMSRole>> wServerRst = null;
            if (wIsTop == 1 && wDepartmentID.size() == 1) {
                wServerRst = wBMSService.BMS_GetRoleList(wLoginUser, wDepartmentID.get(0), wIsTop, wName, wUserID,
                        wActive, wPagination);
            } else {
                wServerRst = wBMSService.BMS_GetRoleList(wLoginUser, wDepartmentID, wName, wUserID, wActive,
                        wPagination);
            }

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/Select")
    Object Select(HttpServletRequest request) {

        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wRoleID = StringUtils.parseInt(request.getParameter("role_id"));

            ServiceResult<List<BMSRoleItem>> wServerRst = wBMSService.BMS_QueryFunctionListByRoleID(wBMSEmployee,
                    wRoleID);

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/UserAll")
    Object UserAll(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wRoleID = StringUtils.parseInt(request.getParameter("role_id"));

            ServiceResult<List<BMSRoleItem>> wServerRst = wBMSService.BMS_QueryUserListByRoleID(wBMSEmployee, wRoleID);

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/Active")
    Object Active(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            List<BMSRole> wBMSRoleItemList = CloneTool.CloneArray(wParam.get("data"), BMSRole.class);

            int wActive = wParam.containsKey("active") ? (int) wParam.get("active") : 0;
            if (wActive != 1) {
                wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;
            }
            ServiceResult<Integer> wServerRst = new ServiceResult<Integer>(0);
            for (BMSRole wBMSRoleItem : wBMSRoleItemList) {
                if (wBMSRoleItem.ID <= 0) {
                    continue;
                }

                if (wActive == 1) {
                    wServerRst = wBMSService.BMS_ActiveRole(wBMSEmployee, wBMSRoleItem.ID);
                } else {
                    wServerRst = wBMSService.BMS_DisableRole(wBMSEmployee, wBMSRoleItem.ID);
                }
                if (StringUtils.isNotEmpty(wServerRst.getFaultCode()))
                    break;

            }
            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst.getResult());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, wServerRst.getResult());
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/Update")
    Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BMSRole wBMSRole = CloneTool.Clone(wParam.get("data"), BMSRole.class);
            if (wBMSRole == null) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            ServiceResult<BMSRole> wServerRst = new ServiceResult<BMSRole>(wBMSRole);

            if (wBMSRole.ID > 0) {
                ServiceResult<Integer> wServiceResult = wBMSService.BMS_SaveRole(wBMSEmployee, wBMSRole);
                wServerRst.FaultCode = wServiceResult.getFaultCode();

            } else {
                wServerRst = wBMSService.BMS_AddRole(wBMSEmployee, wBMSRole);
            }

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBMSRole);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, wBMSRole);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/Delete")
    Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BMSRole wBMSRole = CloneTool.Clone(wParam.get("data"), BMSRole.class);
            if (wBMSRole == null) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            ServiceResult<Integer> wServerRst = wBMSService.BMS_DeleteRole(wBMSEmployee, wBMSRole);

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBMSRole);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, wBMSRole);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/UpdateSelect")
    Object UpdateSelect(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data") || !wParam.containsKey("RoleID")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            List<BMSRoleItem> wBMSRoleItemList = CloneTool.CloneArray(wParam.get("data"), BMSRoleItem.class);

            int wRoleID = StringUtils.parseInt(wParam.get("RoleID"));

            ServiceResult<Integer> wServerRst = wBMSService.BMS_SaveRoleFunctionList(wBMSEmployee, wRoleID,
                    wBMSRoleItemList);

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/UpdateUser")
    Object UpdateUser(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data") || !wParam.containsKey("RoleID")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            List<BMSRoleItem> wBMSRoleItemList = CloneTool.CloneArray(wParam.get("data"), BMSRoleItem.class);
            int wRoleID = StringUtils.parseInt(wParam.get("RoleID"));
            ServiceResult<Integer> wServerRst = wBMSService.BMS_SaveRoleUserList(wBMSEmployee, wRoleID,
                    wBMSRoleItemList);

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/UserAllByFunctionID")
    Object UserAllByFunctionID(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wFunctionID = StringUtils.parseInt(request.getParameter("FunctionID"));

            ServiceResult<List<BMSRoleItem>> wServerRst = wBMSService.BMS_QueryUserListByFunctionID(wBMSEmployee,
                    wFunctionID);
            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/Check")
    Object Check(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            int wUserID = StringUtils.parseInt(request.getParameter("UserID"));

            int wAuthortyID = StringUtils.parseInt(request.getParameter("AuthortyID"));

            int wRangeID = StringUtils.parseInt(request.getParameter("RangeID"));

            int wTypeID = StringUtils.parseInt(request.getParameter("TypeID"));

            ServiceResult<Integer> wServerRst = wBMSService.BMS_CheckPower(wBMSEmployee, wUserID, wAuthortyID, wRangeID,
                    wTypeID);

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst.getResult());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, wServerRst.getResult());
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, false);
        }
        return wResult;
    }

    @GetMapping("/FunctionAll")
    Object FunctionAll(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wLoginID = wBMSEmployee.getID();

            int wOperatorID = StringUtils.parseInt(request.getParameter("OperatorID"));

            if (wOperatorID <= 0)
                wOperatorID = wLoginID;

            ServiceResult<List<BMSRoleItem>> wServerRst = wBMSService.BMS_GetFunctionListByLoginID(wBMSEmployee,
                    wOperatorID);

            List<Integer> wDistin = new ArrayList<Integer>();
            if (wServerRst != null && wServerRst.getResult() != null && wServerRst.getResult().size() > 0) {
                wDistin = wServerRst.getResult().stream().map(p -> p.FunctionID).distinct()
                        .collect(Collectors.toList());
            }

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), wDistin);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(),
                        wDistin);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, false);
        }
        return wResult;
    }

    @GetMapping("/UserRoleByFunctionID")
    Object UserRoleByFunctionID(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wUserID = wBMSEmployee.getID();
            int wFunctionID = StringUtils.parseInt(request.getParameter("FunctionID"));

            List<BMSRoleItem> wBMSRoleItemList = new ArrayList<BMSRoleItem>();
            ServiceResult<List<BMSRole>> wBMSRoleServerRst = wBMSService.BMS_GetRoleList(wBMSEmployee, null, "", -1, 1,
                    Pagination.getMaxSize());
            if (wBMSRoleServerRst.getResult() != null) {
                for (BMSRole wBMSRole : wBMSRoleServerRst.getResult()) {
                    ServiceResult<List<BMSRoleItem>> wFunctionServerRst = wBMSService
                            .BMS_QueryFunctionListByRoleID(wBMSEmployee, wBMSRole.ID);
                    wBMSRoleServerRst.FaultCode += wFunctionServerRst.FaultCode;
                    if (wFunctionServerRst.getResult() == null || wFunctionServerRst.getResult().size() == 0)
                        continue;
                    if (wFunctionServerRst.getResult().stream().filter(p -> p.FunctionID == wFunctionID).findFirst()
                            .isPresent()) {
                        ServiceResult<List<BMSRoleItem>> wUserServerRst = wBMSService
                                .BMS_QueryUserListByRoleID(wBMSEmployee, wBMSRole.ID);
                        wBMSRoleServerRst.FaultCode += wUserServerRst.FaultCode;
                        if (wUserServerRst.getResult() != null && wUserServerRst.getResult().size() > 0)
                            wBMSRoleItemList.addAll(wUserServerRst.getResult());
                    }
                }
            }

            List<BMSRoleItem> wServerRst = wBMSRoleItemList.stream().filter(p -> p.FunctionID == wUserID)
                    .collect(Collectors.toList());

            if (StringUtils.isEmpty(wBMSRoleServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wBMSRoleServerRst.getFaultCode(), null, wServerRst);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/RoleRangeAll")
    Object RoleRangeAll(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            int wRoleID = StringUtils.parseInt(request.getParameter("RoleID"));

            ServiceResult<List<BMSRoleItem>> wServerRst = wBMSService.BMS_QueryRangeListByRoleID(wBMSEmployee, wRoleID);

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/FunctionRangeAll")
    Object FunctionRangeAll(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wUserID = wBMSEmployee.getID();

            int wFunctionID = StringUtils.parseInt(request.getParameter("FunctionID"));
            int wOperatorID = StringUtils.parseInt(request.getParameter("OperatorID"));

            if (wOperatorID <= 0)
                wOperatorID = wUserID;
            ServiceResult<List<BMSRoleItem>> wServerRst = wBMSService.BMS_QueryRangeListByOperatorID(wBMSEmployee,
                    wOperatorID, wFunctionID);

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/UpdateRoleRange")
    Object UpdateRoleRange(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data") || !wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            int wRoleID = StringUtils.parseInt(wParam.get("RoleID"));
            List<BMSRoleItem> wBMSRoleItemList = CloneTool.CloneArray(wParam.get("data"), BMSRoleItem.class);

            ServiceResult<Integer> wServerRst = wBMSService.BMS_SaveRoleRangeList(wBMSEmployee, wRoleID,
                    wBMSRoleItemList);

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 导入
     */
    @PostMapping("/Import")
    public Object Import(HttpServletRequest request, @RequestParam("file") MultipartFile[] files) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            if (files.length == 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, "提示：没有要上传的Excel文件！");
            }

            ServiceResult<Integer> wServiceResult = new ServiceResult<>();
            ServiceResult<ExcelData> wExcelData = null;
            String wOriginalFileName = null;
            for (MultipartFile wMultipartFile : files) {
                wOriginalFileName = wMultipartFile.getOriginalFilename();

                assert wOriginalFileName != null;
                if (wOriginalFileName.contains("xlsx"))
                    wExcelData = ExcelReader.getInstance().readMultiSheetExcel(wMultipartFile.getInputStream(),
                            wOriginalFileName, "xlsx", 1000000);
                else if (wOriginalFileName.contains("xls"))
                    wExcelData = ExcelReader.getInstance().readMultiSheetExcel(wMultipartFile.getInputStream(),
                            wOriginalFileName, "xls", 1000000);

                assert wExcelData != null;
                if (StringUtils.isNotEmpty(wExcelData.FaultCode)) {
                    wResult = GetResult(RetCode.SERVER_CODE_ERR, wExcelData.FaultCode);
                    return wResult;
                }

                wServiceResult = wBMSService.BMS_ImportRole(wLoginUser, wExcelData.Result);

                if (!StringUtils.isEmpty(wServiceResult.FaultCode))
                    break;
            }

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "导入成功!", null, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


}
